package org.xdq.controller;

import java.util.List;

import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.xdq.base.Constants;
import org.xdq.base.CurrUser;
import org.xdq.base.Result;
import org.xdq.dto.MenuDto;
import org.xdq.service.HomeService;

@RestController
public class HomeController {
	
	@Autowired
	private HomeService homeService;
	
	/*
	 	restful风格，简单理解如下：
	 		get请求：查询
	 		post请求：向服务器投送数据
	 		put请求：更新服务器数据，同一请求发送多次效果一至
	 		delete请求：从服务器删除数据
	 		
	 */
	
	//@RequestMapping(value = "/menu",method = RequestMethod.GET)
	@GetMapping("/menu")
	public List<MenuDto> menuList(@SessionAttribute(Constants.SESSION_ATTR_CURR_USER) CurrUser currUser){
		
		return homeService.getMenuList(currUser.getUserId());
		
	}
	
	//获取当前用户
	@GetMapping("/user")
	public Object currUser(HttpSession session) {
		
		return session.getAttribute(Constants.SESSION_ATTR_CURR_USER);
		
	}
	
	//退出系统
	@DeleteMapping("/user")
	public Result logout(HttpSession session) {
		session.removeAttribute(Constants.SESSION_ATTR_CURR_USER);
		return Result.success("您已成功退出系统！");
	}

}
